package com.wulis.provider.basic.service.impl;

import java.util.List;

import com.wulis.common.model.enums.DeletedStateEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wulis.common.support.BaseServiceImpl;
import com.wulis.provider.basic.mapper.OrganizationMapper;
import com.wulis.provider.basic.model.dto.InsertOrganizationDto;
import com.wulis.provider.basic.model.dto.QueryOrganizationPageDto;
import com.wulis.provider.basic.model.dto.UpdateOrganizationDto;
import com.wulis.provider.basic.model.entity.Organization;
import com.wulis.provider.basic.model.vo.QueryOrganizationListVo;
import com.wulis.provider.basic.model.vo.QueryOrganizationPageVo;
import com.wulis.provider.basic.model.vo.QueryOrganizationVo;
import com.wulis.provider.basic.service.DepartmentService;
import com.wulis.provider.basic.service.OrganizationService;

import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 基础信息管理:组织机构表 Service实现类
 *
 * @author WuliBao
 * @date 2020-05-21
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrganizationServiceImpl extends BaseServiceImpl<OrganizationMapper, Organization>
    implements OrganizationService {
    
    private final DepartmentService departmentService;
    
    private final OrganizationMapper organizationMapper;
    
    /**
     * WEB:查询机构列表(分页)
     *
     * @param queryOrganizationPageDto queryOrganizationPageDto
     * @return IPage
     */
    @Override
    public IPage<QueryOrganizationPageVo> queryOrganizationPage(
        @NonNull QueryOrganizationPageDto queryOrganizationPageDto) {
        Page<QueryOrganizationPageVo> page =
            new Page<>(queryOrganizationPageDto.getPageNumber(), queryOrganizationPageDto.getPageSize());
        return organizationMapper.queryOrganizationPage(page,
            queryOrganizationPageDto.getOrganizationName(),
            queryOrganizationPageDto.getEnable());
    }
    
    /**
     * WEB:查询机构列表(全量)
     *
     * @param enable enable
     * @return List
     */
    @Override
    public List<QueryOrganizationListVo> queryOrganizationList(Integer enable) {
        return organizationMapper.queryOrganizationList(enable);
    }
    
    /**
     * WEB:查询机构信息
     *
     * @param id id
     * @return QueryOrganizationVo
     */
    @Override
    public QueryOrganizationVo queryOrganization(@NonNull Long id) {
        return organizationMapper.queryOrganization(id);
    }
    
    /**
     * WEB:新增机构信息
     *
     * @param insertOrganizationDto insertOrganizationDto
     * @return Integer
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertOrganization(@NonNull InsertOrganizationDto insertOrganizationDto) {
        // 新增机构
        Organization organization = new Organization();
        BeanUtils.copyProperties(insertOrganizationDto, organization);
        return organizationMapper.insert(organization);
    }
    
    /**
     * WEB:修改机构信息
     *
     * @param updateOrganizationDto updateOrganizationDto
     * @return Integer
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateOrganization(@NonNull UpdateOrganizationDto updateOrganizationDto) {
        // 修改机构
        Organization organization = new Organization();
        BeanUtils.copyProperties(updateOrganizationDto, organization);
        return organizationMapper.updateById(organization);
    }
    
    /**
     * WEB:删除机构信息
     *
     * @param id id
     * @return Integer
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteOrganization(@NonNull Long id) {
        // 删除机构
        Organization organization = new Organization();
        organization.setId(id);
        organization.setDeleted(DeletedStateEnum.IS_DELETED.value());
        int update = organizationMapper.updateById(organization);
        // 删除组织机构下的所有部门
        departmentService.deleteDepartmentByOrganizationId(id);
        return update;
    }
}
